<?php

require("config.php");
mysql_connect(DB_HOST, DB_USER, DB_PASS);
mysql_select_db(DB_NAME);

if ($_REQUEST["act"] == "lookup") {
	$q = mysql_real_escape_string($_REQUEST["q"]);
	$query = "SELECT word,partofspeech,definition FROM dictionary WHERE word LIKE '" . $q . "%'";
	$query = mysql_query($query);
	$exist = false;
	while ($row = mysql_fetch_assoc($query)) {
		$exist = true;
		echo "<strong>" . $row["word"] . "</strong> <em>(" . $row["partofspeech"] . ")</em> : " . $row["definition"] . "<div class=\"sep\"></div>";
	}
	if (!$exist) {
		echo "Sorry, no words matched your query.";
		if (ALLOW_ADD) {
			echo "<br/>You can add a definition, if you would like:
			<form method=\"post\" id=\"theformtwo\" onsubmit=\"addDefinition(); return false;\" action=\"./\">
				Part of speech: <select name=\"partofspeech\">
					<option value=\"v\">Verb</option>
					<option value=\"n\">Noun</option>
					<option value=\"pro\">Pronoun</option>
					<option value=\"adj\">Adjective</option>
					<option value=\"adv\">Adverb</option>
					<option value=\"pre\">Preposition</option>
					<option value=\"c\">Conjunction</option>
					<option value=\"i\">Interjection</option>
				</select><br/>
				Definition: <textarea name=\"definition\" cols=\"30\" rows=\"10\"></textarea>
				<input type=\"submit\" name=\"go\" value=\"Add Word\" />
				<input type=\"hidden\" name=\"act\" value=\"add\" />
			</form>";
		}
	}
} elseif ($_REQUEST["act"] == "add") {
	$q = mysql_real_escape_string($_REQUEST["q"]);
	if ($check[0]) {
		echo "That word already exists.";
	} else {
		$query = "INSERT INTO dictionary VALUES (
			NULL,
			'" . mysql_real_escape_string($_REQUEST["q"]) . "',
			'" . mysql_real_escape_string($_REQUEST["partofspeech"]) . "',
			'" . mysql_real_escape_string($_REQUEST["definition"]) . "')";
		$query = mysql_query($query);
		if ($query) {
			echo "Your word has been added successfully.";
		} else {
			echo "There was an error adding your word.";
		}
	}
}

?>
